package com.yunfeisoft.business.dao.impl.postgres;

import com.applet.base.ServiceDaoImpl;
import com.applet.sql.builder.SelectBuilder;
import com.applet.sql.builder.WhereBuilder;
import com.applet.utils.Page;
import com.yunfeisoft.business.dao.inter.SmsFlashPromotionProductRelationDao;
import com.yunfeisoft.business.model.PmsProduct;
import com.yunfeisoft.business.model.SmsFlashPromotion;
import com.yunfeisoft.business.model.SmsFlashPromotionProductRelation;
import com.yunfeisoft.business.model.SmsFlashPromotionSession;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * ClassName: SmsFlashPromotionProductRelationDaoImpl
 * Description: 商品限时购与商品关系表Dao实现
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Repository
public class SmsFlashPromotionProductRelationDaoImpl extends ServiceDaoImpl<SmsFlashPromotionProductRelation, String> implements SmsFlashPromotionProductRelationDao {

    @Override
    public Page<SmsFlashPromotionProductRelation> queryPage(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        if (params != null) {
            initPageParam(wb, params);
            wb.andFullLike("p.name", params.get("productName"));
        }

        SelectBuilder builder = getSelectBuilder("pp");
        builder.column("fp.title as flashPromotionTitle")
                .column("fps.name as flashPromotionSessionName")
                .column("p.name as productName")
                .column("p.price as productPrice")
                .join(SmsFlashPromotion.class).alias("fp").on("pp.flashPromotionId = fp.id").build()
                .join(SmsFlashPromotionSession.class).alias("fps").on("pp.flashPromotionSessionId = fps.id").build()
                .join(PmsProduct.class).alias("p").on("pp.productId = p.id").build();

        return queryPage(builder.getSql(), wb);
    }

    @Override
    public boolean isDupProduct(String id, String flashPromotionId, String flashPromotionSessionId, String productId) {
        WhereBuilder wb = new WhereBuilder();
        wb.andEquals("flashPromotionId", flashPromotionId);
        wb.andEquals("flashPromotionSessionId", flashPromotionSessionId);
        wb.andEquals("productId", productId);
        wb.andNotEquals("id", id);
        return count(wb) > 0;
    }

    /**
     * 查询秒杀商品列表
     * @return
     */
    @Override
    public List<SmsFlashPromotionProductRelation> queryFlashPromotionProduct() {
        WhereBuilder wb = new WhereBuilder();
        wb.andGreaterEquals("f.endDate", new Date());
        wb.andLessEquals("f.startDate", new Date());
        wb.andEquals("f.onlineStatus", 1);
        wb.setOrderByWithDesc("r.createTime");
        SelectBuilder builder = getSelectBuilder("r");
        builder.column("p.name as productName")
                .column("p.price as productPrice")
                .join(PmsProduct.class)
                .alias("p")
                .on("r.productId = p.id").build()
                .join(SmsFlashPromotion.class)
                .alias("f")
                .on("f.id = r.flashPromotionId").build();

        return query(builder.getSql(), wb);
    }

    /**
     * 查询首页秒杀商品列表
     * @return
     */
    @Override
    public List<SmsFlashPromotionProductRelation> queryFlashPromotionProductIndex(int limit) {
        WhereBuilder wb = new WhereBuilder();
        // 获取当前时间
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        wb.andGreaterEquals("fps.endDate", date);
        wb.andLessEquals("fps.startDate", date);
        SelectBuilder builder = getSelectBuilder("fppr")
                .column("p.name as productName")
                .column("p.price as productPrice")
                .join(SmsFlashPromotionSession.class)
                .alias("fps")
                .on("fppr.flashPromotionSessionId = fps.id").build()
                .join(PmsProduct.class)
                .alias("p")
                .on("fppr.productId = p.id").build();
        if (limit > 0) {
            wb.setPageNum(1);
            wb.setPageSize(limit);
            return queryLimit(builder.getSql(), wb);
        }
        return query(builder.getSql(), wb);
    }
}